package zabbix

import (
	"encoding/json"
	"errors"
)

func UsergroupCreateData() (string,map[string]interface{}) {
	method := "usergroup.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "name": "Operation managers",
        "rights": {
            "permission": 0,
            "id": "2"
        },
        "userids": "12"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func UsergroupDeleteData() (string,map[string]interface{}) {
	method := "usergroup.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "20",
        "21"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func UsergroupGetData() (string,map[string]interface{}) {
	method := "usergroup.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend",
        "status": 0
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func UsergroupUpdateData() (string,map[string]interface{}) {
	method := "usergroup.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "usrgrpid": "17",
        "users_status": "1"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}



func UsergroupCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := UsergroupCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func UsergroupDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := UsergroupDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func UsergroupGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := UsergroupGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func UsergroupUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := UsergroupUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}


